package com.zw.mes.module.tecsummary.service;

import com.zw.mes.entity.technology.*;
import com.zw.mes.module.tecsummary.mapper.SummaryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 * @author mayongfeng
 * @version 1.0.0
 * @ClassName SummaryService.java
 * @Description TODO
 * @createTime 2021年03月29日 09:48:00
 */
@Service
@SuppressWarnings("all")
public class SummaryService {

    @Autowired
    private SummaryMapper summaryMapper;


    /**
     * @Author mayongfeng
     * @Date 14:31 2021/5/6
     * @Description 查询技术汇总列表页面
     *
     **/
    public List<ReceiveSum> findList(ReceiveSum receiveSum) {
        List<ReceiveSum> receiveSumList = summaryMapper.findList(receiveSum);
        return receiveSumList;

    }

    /** 51=提报计划*/
    public List<Result> reportSee(Result result) {
        return summaryMapper.reportSee(result);
    }

    /** 52=制作件清单*/
    public List<MakeResult> makeSee(MakeResult makeResult) {
        return summaryMapper.makeSee(makeResult);
    }

    /** 53=图纸变更单*/
    public List<MakeResult> drawSee(DrawSheet drawSheet) {
        return summaryMapper.drawSee(drawSheet);
    }

    /** 54=涂装设计清单*/
    public List<DsnCoatSheet> paintSee(DsnCoatSheet dsnCoatSheet) {
        return summaryMapper.paintSee(dsnCoatSheet);
    }

    /** 55=工艺卡列表*/
    public List<Tecard> cardSee(Tecard tecard) {
        return summaryMapper.cardSee(tecard);
    }

    /** 56=工艺路线*/
    public List<Tecard> routeSee(Tecard tecard) {
        List<Tecard> tecardList = summaryMapper.routeSeeALl(tecard);
        List<Tecard> tecardList1 = summaryMapper.routeSee(tecard);
        for (int i = 0; i < tecardList1.size(); i++) {
            for (int j = 0; j < tecardList.size(); j++) {
                int a = tecardList1.get(i).getId();
                int b= tecardList.get(j).getId();
                if (a==b){
                    tecardList.get(j).setRoute((null==tecardList.get(j).getRoute()?" ":tecardList.get(j).getRoute()) +"  "+tecardList.get(j).getCou()+"、"+ tecardList1.get(i).getRoute());
                    tecardList.get(j).setCou(tecardList.get(j).getCou()+1);
                }
            }
        }
        return tecardList;
    }

    /** 57=机加工时*/
    public List<Tecard> machHoursSee(Tecard tecard) {
        return summaryMapper.machHoursSee(tecard);
    }

    /** 58=机加工时汇总*/
    public List<Tecard> summaryHoursSee(Tecard tecard) {
        List<Tecard> tecardList = summaryMapper.summaryHoursSee(tecard);
        List<Tecard> tecardList1 = summaryMapper.summaryHoursSee1(tecard);
        for (int i = 0; i < tecardList.size(); i++) {
            for (int j = 0; j < tecardList1.size(); j++) {
                if (tecardList.get(i).getProdNo().equals(tecardList1.get(j).getProdNo()) &&
                        tecardList.get(i).getProName().equals(tecardList1.get(j).getProName()) &&
                        tecardList.get(i).getPart().equals(tecardList1.get(j).getPart()) &&
                        tecardList.get(i).getProcess().equals(tecardList1.get(j).getProcess())

                ) {
                    if ("".equals(tecardList.get(i).getEquName()) && "".equals(tecardList1.get(j).getEquName())) {
                        double dou = sumGS(tecardList.get(i).getTotal(), tecardList1.get(j).getWorkTimeOne(), tecardList1.get(j).getWorkTimeTwo());
                        tecardList.get(i).setTotal(dou);
                    } else if (tecardList.get(i).getEquName().equals(tecardList1.get(j).getEquName())) {
                        double dou = sumGS(tecardList.get(i).getTotal(), tecardList1.get(j).getWorkTimeOne(), tecardList1.get(j).getWorkTimeTwo());
                        tecardList.get(i).setTotal(dou);
                    }
                }
            }
        }
        return tecardList;

    }

    /** 查询主单*/
    public DrawSheet  getDrawSheet(String id) {
        return summaryMapper.getDrawSheet(id);
    }

    /** 查询从表*/
    public List<DrawSheet> getDrawDetail(String id) {
        return summaryMapper.getDrawDetail(id);
    }

    private double sumGS(double a, double b, double c) {
        double mm = aa(a, b);
        double reslut = aa(mm, c);

        return reslut;
    }

    public static Double aa(double a, double b) {
        if (a==0){
            return  b;
        }
        if (b==0){
            return  a;
        }
        int aa = (int) a * 100 / 100;
        int bb = (int) b * 100 / 100;
        int cc = (int) (Math.round(a * 100) % 100) + (int) (b * 100 % 100);
        return Double.parseDouble(aa + bb + cc / 60 + "." + (cc % 60 >=10? cc % 60:("0"+cc % 60)));

    }


    public DrawSheet getDrawUrlDetail(Integer id) {
        return summaryMapper.getDrawUrlDetail(id);
    }
}
